﻿using ApiIOCService.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ApiIOCService.Implement
{
    class ApplicationLogService : IApplicationLogService
    {
        string CheckCreateFilePath(string filePath, string fileName)
        {
            if (!Directory.Exists(filePath))
            {
                lock (filePath)
                {
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                }
            }
            return filePath + fileName;
        }
        void WriteInfo(string filePath, string fileName, string message)
        {
            try
            {
                var path = CheckCreateFilePath(filePath, fileName);
                lock (path)
                {
                    File.AppendAllText(path, message + "\r\n", Encoding.UTF8);
                }
            }
            catch (Exception ex)
            {

            }
        }
        public void Info(string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Info.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "\r\n" + message);
            });
            task.Start();
        }

        public void Info(string appid, string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Info.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "       " + appid + "\r\n" + message);
            });
            task.Start();
        }

        public void Debug(string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Debug.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "\r\n" + message);
            });
            task.Start();
        }

        public void Debug(string appid, string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Debug.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "       " + appid + "\r\n" + message);
            });
            task.Start();
        }

        public void Error(string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Error.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "\r\n" + message);
            });
            task.Start();
        }

        public void Error(string appid, string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Error.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "       " + appid + "\r\n" + message);
            });
            task.Start();
        }

        public void Error(string message, Exception ex)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Error.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "\r\n" + message + "\r\n" + ex.Message);
            });
            task.Start();
        }

        public void Error(string appid, string message, Exception ex)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Error.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "       " + appid + "\r\n" + message + "\r\n" + ex.Message);
            });
            task.Start();
        }

        public void Warn(string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Warn.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "\r\n" + message);
            });
            task.Start();
        }

        public void Warn(string appid, string message)
        {
            Task task = new Task(() =>
            {
                WriteInfo("C://WebAppLog//" + DateTime.Now.ToString("yyyyMMddHH") + "//", "Warn.txt", "=======================================================\r\n" + DateTime.Now.ToString() + "       " + appid + "\r\n" + message);
            });
            task.Start();
        }
    }
}
